package com.systemic.service.impl;

import com.systemic.mapper.GroupCreateApplyMapper;
import com.systemic.mapper.GroupInfoMapper;
import com.systemic.mapper.GroupRelationMapper;
import com.systemic.pojo.GroupCreateApply;
import com.systemic.pojo.GroupInfo;
import com.systemic.pojo.GroupRelation;
import com.systemic.service.GroupCreateApplyService;
import com.systemic.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: GroupCreateApplyServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-02-02 10:07
 * @Version:
 **/
@Service
public class GroupCreateApplyServiceImpl implements GroupCreateApplyService {

    @Resource
    private GroupCreateApplyMapper groupCreateApplyMapper;

    @Resource
    private GroupInfoMapper groupInfoMapper;

    @Resource
    private GroupRelationMapper groupRelationMapper;

    /**
     * 团队创建申请
     *
     * @param groupCreateApply
     * @return
     */
    @Override
    @Transactional
    public int createApply(GroupCreateApply groupCreateApply) {
        int row = 0;
        groupCreateApply.setGmtCreate(new Date());
        row = groupCreateApplyMapper.insert(groupCreateApply)+row;
        return row;
    }

    /**
     * 审批列表（管理端）
     *
     * @param pageVo
     * @return
     */
    @Override
    public List<GroupCreateApply> selectList(PageVo pageVo) {
        // 获取当前页数和每页显示的数据条数
        Integer pageNo = pageVo.getPageNo();
        Integer pageSize = pageVo.getPageSize();
        // 计算OFFSET值
        Integer offset = (pageNo - 1) * pageSize;
        return groupCreateApplyMapper.selectListPage(offset,pageSize);
    }

    /**
     * 审批通过（管理端）
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int agree(Integer id) {
        int row = 0;
        GroupCreateApply groupCreateApply = new GroupCreateApply();
        groupCreateApply.setId(id);
        GroupCreateApply res = groupCreateApplyMapper.selectONE(groupCreateApply);
        if (res!=null){
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setFounder(res.getFounder());
            groupInfo.setGroupName(res.getGroupName());
            groupInfo.setGroupInfo(res.getGroupInfo());
            groupInfo.setNumLim(res.getNumLim());
            groupInfo.setGmtCreate(res.getGmtCreate());

            row = groupInfoMapper.insert(groupInfo)+row;
            row = groupCreateApplyMapper.delete(res)+row;

            // 获取团体id
            Integer GroupId = groupInfoMapper.getLastInsertId();
            GroupRelation groupRelation = new GroupRelation();
            groupRelation.setGroupId(GroupId);
            groupRelation.setUid(res.getFounder());
            groupRelation.setJoinTime(new Date());
            groupRelation.setStatus(1);   // status = 1 表示创建者
            row = groupRelationMapper.insert(groupRelation)+row;
        }
        return row;
    }

    /**
     * 审批忽略（管理端）
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public int disagree(Integer id) {
        int row = 0;
        GroupCreateApply groupCreateApply = new GroupCreateApply();
        groupCreateApply.setId(id);
        GroupCreateApply res = groupCreateApplyMapper.selectONE(groupCreateApply);
        if (res!=null){
            row = groupCreateApplyMapper.delete(res)+row;
        }
        return row;
    }
}
